package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.RoleDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleDao roleDao;

    @Override
    public PageInfo<Role> findPage(QueryPageBean queryPageBean) {

        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());

        PageInfo pageInfo = new PageInfo(page);
        return pageInfo;
    }

    @Override
    public List<Permission> findPermission() {
        return roleDao.findPermission();
    }

    @Override
    public List<Menu> findMenu() {
        return roleDao.findMenu();
    }

    @Override
    public void addRole(Role role, Integer[] permissionIds, Integer[] menuIds) {

        Integer count = roleDao.addRole(role);


        if (count>0){
            Integer roleId = role.getId();
            for (Integer permissionId : permissionIds) {
                roleDao.addPermissionIds(roleId,permissionId);
            }
            for (Integer menuId : menuIds) {
                roleDao.addMenuIds(roleId,menuId);
            }
        }
    }

    @Override
    public Role findRoleByIdBackRole(Integer roleId) {
        return roleDao.findRoleByIdBackRole(roleId);
    }

    @Override
    public List<Integer> findRolePermissionByRoleId(Integer roleId) {
        return roleDao.findRolePermissionByRoleId(roleId);
    }

    @Override
    public List<Integer> findRoleMenuByRoleId(Integer roleId) {
        return roleDao.findRoleMenuByRoleId(roleId);
    }

    @Override
    public void updateByRole(Role role) {
        roleDao.updateByRole(role);
    }

    @Override
    public void updateBypermissionIds(Integer roleId, Integer[] permissionIds) {
        //先删除原来的选项
        roleDao.updateBypermissionIdsDelete(roleId);
        //在添加权限id
        for (Integer permissionId : permissionIds) {
            roleDao.updateBypermissionIds(roleId,permissionId);
        }
    }

    @Override
    public void updateBymenuIds(Integer roleId, Integer[] menuIds) {
        roleDao.updateBymenuIdsDelete(roleId);
        for (Integer menuId : menuIds) {
            roleDao.updateBymenuIds(roleId,menuId);
        }
    }

    @Override
    public void deleteById(Integer id) {
        roleDao.deleteRolePermission(id);
        roleDao.deleteRoleMenu(id);
        roleDao.deleteById(id);
    }

    @Override
    public List<String> findkeywords() {
        return roleDao.findkeywords();
    }


}
